#include <iostream>
#include <algorithm>
#include <sstream>
#include <typeinfo>
#include <string.h>
#include <memory>
#include <fstream>
#include "ojData.h"
#include "jsonObject.h"
using namespace std;

#define kvpair(x) make_pair(x, #x)
std::unordered_map<int, std::string> codeToStr = {
    kvpair(WA), kvpair(PE), kvpair(TLE),
    kvpair(MLE), kvpair(CE), kvpair(SYS_ERROR), kvpair(OLE), kvpair(SYS_CHECK),
    kvpair(CONTINUE), kvpair(RUN_OK), kvpair(RE), kvpair(RUN_FAIL),
    kvpair(SPJFILE_COMPILE_ERROR), kvpair(CETLE), kvpair(LESS_STDFILES), 
    kvpair(LESS_SPJFILE), kvpair(SPJFILE_RUN_ERROR), kvpair(PIPE_ERROR),
    kvpair(PARAMS_ERROR), kvpair(PIPE_ERROR), kvpair(MYSQL_ERROR), kvpair(WAIT_JUDGE),
};

std::ostream &operator<<(std::ostream &out, const ProcessStatus &ps)
{
    out << "result: " << codeToStr[ps.result] << "(" << ps.result << ")"
        << ",time: " << ps.time << ",memory: " << ps.memory
        << ",error_code: " << ps.errorcode << "," << endl 
        << "error:[" << ps.error << "]," << endl 
        << "output:[" << ps.output << "]," << endl
        << ",extern_info:" << ps.extern_info->json() << endl;
    return out;
}

std::string ProcessStatus::json() const
{
    return JsonObject({
        {"result", JSONOBJECT(codeToStr[result])},
        {"result_code", JSONOBJECT(result)},
        {"time", JSONOBJECT(time)},
        {"memory", JSONOBJECT(memory)},
        {"error_code", JSONOBJECT((int)errorcode)},
        {"error", JSONOBJECT(error)},
        {"output", JSONOBJECT(output)},
        {"extern_info", extern_info}
    })
    .json();
}